package com.ruoyi.web.controller.applet;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BdmOrder;
import com.ruoyi.system.domain.BdmOrderPhysicalDistributionVo;
import com.ruoyi.system.domain.dto.BdmOrderCreateDto;
import com.ruoyi.system.domain.dto.BdmOrderDto;
import com.ruoyi.system.domain.dto.BdmOrderPayDto;
import com.ruoyi.system.domain.vo.applet.AppletBdmOrderVo;
import com.ruoyi.system.domain.vo.applet.AppletOrderDetailsVo;
import com.ruoyi.system.service.IBdmOrderService;
import com.ruoyi.system.service.IDistributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/system/order")
@Validated
public class AppletOrderController extends BaseController
{
    @Autowired
    private IBdmOrderService bdmOrderService;

    @Autowired
    private IDistributionService iDistributionService;



    /**
     * 创建订单
     */
    @PostMapping("/createOrder")
    @Log(title = "创建商品订单", businessType = BusinessType.OTHER)
    public AjaxResult createOrder(@Valid @RequestBody BdmOrderCreateDto bdmOrderCreateDto)
    {
        return success(bdmOrderService.createOrder(bdmOrderCreateDto));
    }

    /**
     * 余额支付
     */
    @PostMapping("/balancePay")
    @Log(title = "商品订单支付成功调用", businessType = BusinessType.OTHER)
    public AjaxResult balancePay(@Valid  @RequestBody BdmOrderPayDto bdmOrderPayDto)
    {
        return bdmOrderService.balancePay(bdmOrderPayDto);
    }

    /**
     * 根据当前用户查询订单列表
     * @return {@link AppletBdmOrderVo}
     */
    @GetMapping("/listByUser")
    public TableDataInfo listByUser(BdmOrderDto bdmOrderDto)
    {
        Long userId = getUserId();
        bdmOrderDto.setUserId(userId);
        //关闭超时订单
        bdmOrderService.closeOrder();
        startPage();
        List<AppletBdmOrderVo> list = bdmOrderService.listByUser(bdmOrderDto);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setTotal(bdmOrderService.lambdaQuery().eq(BdmOrder::getUserId,userId).count());
        return dataTable;
    }

    /**
     * 查询订单详情
     * @return {@link AppletOrderDetailsVo}
     */
    @GetMapping("/orderDetails")
    public AjaxResult orderDetails(@NotBlank(message = "订单号必填") String orderId)
    {
        return success(bdmOrderService.orderDetails(orderId));
    }



    /**
     * 订单退单
     * @param bdmOrderDto
     * @return
     */
    @GetMapping("/chargeback")
    public AjaxResult chargeback(@Valid BdmOrderDto bdmOrderDto) {
        return bdmOrderService.chargeback(bdmOrderDto);
    }

    /**
     * 订单收货
     * @param bdmOrderDto
     * @return
     */
    @GetMapping("/receivingGoods")
    public AjaxResult receivingGoods(BdmOrderDto bdmOrderDto) {
        return toAjax(bdmOrderService.receivingGoods(bdmOrderDto));
    }

    /**
     * 取消订单
     */
    @GetMapping("/cancelOrder")
    public AjaxResult cancelOrder(@NotBlank(message = "订单号必填") String orderId) {
        return toAjax(bdmOrderService.cancelOrder(orderId));
    }

    /**
     * 根据订单号查询订单轨迹
     */
    @GetMapping("/getOrderPhysicalDistribution")
    public TableDataInfo getOrderPhysicalDistribution(@RequestParam("orderId") String orderId) {
        BdmOrder bdmOrder = bdmOrderService.getById(orderId);
        HashMap<String, String> map = new HashMap<>();
        map.put("shipperCode", bdmOrder.getLcId());
        map.put("logisticCode", bdmOrder.getLId());
        return getDataTable(iDistributionService.getOrderPhysicalDistribution(map).stream().sorted(Comparator.comparing(BdmOrderPhysicalDistributionVo::getAcceptTime).reversed()).collect(Collectors.toList()));
    }

    /**
     * 用户取消退单
     */
    @GetMapping("/cancelRefund")
    public AjaxResult cancelRefund(@Valid @NotBlank(message = "订单号必填") String orderId) {
        return bdmOrderService.cancelRefund(orderId);
    }

    /**
     * 获取订单统计信息
     * @param userId
     * @return
     */
    @GetMapping("/getOrderStatistics/{userId}")
    public AjaxResult getOrderStatistics(@PathVariable Long userId) {
        return bdmOrderService.getOrderStatistics(userId);
    }
}
